﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VideoClassLibrary.BusinessLogicLayer;
using System.Data;
using VideoClassLibrary.Extension;

//System.Web.UI.Page
public partial class CourseListManagement_SearchCourseAllList :AdminUserBase 
{
    CourseListModule CM = new CourseListModule();
    public string courselisthtml;
    string courseListId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        showcourseList();
        if (!this.IsPostBack)
        {

            bindPageData();
            
        }
        
    }
    /// <summary>
    /// 读取全部课程名称
    /// </summary>
    private void showcourseList()
    {
        if (!String.IsNullOrEmpty(Request.QueryString["courseListId"] ))
        {
            courseListId = Request.QueryString["courseListId"];
        }
        courselisthtml = courseListId == "" ? "<a href=\"?\"><span>全部课程名称<span></a>" : "<a href=\"?\"><span>全部课程名称</span></a>";
        int id = this.CurrentUser.SchoolID;
        DataSet ds = CM.QueryByCourseNamebySchoolid(id);
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            courselisthtml += "<a href=\"?courseListId=" +dr["id"].ToString() + "\"/><span>";
            courselisthtml += dr["vc_name"].ToString();
            courselisthtml += "</span></a>";
        }
        if (Request.QueryString["courseListId"] != "")
        {

            courseListId = Request.QueryString["courseListId"];
        }
    }
    /// <summary>
    /// 根据选择删除数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbdelete_Command(object sender, CommandEventArgs e)
    {
       
        int id = int.Parse(e.CommandArgument.ToString());
        CM.DeleteCourseList(id);
        Response.Redirect("SearchCourseAllList.aspx"); 

    }
    protected void pager_PageChanged(object sender, EventArgs e)
    {
        bindPageData();
    }

    private void bindPageData()
    {
        int schoolid = this.SchoolID;
        DataSet ds = CM.QueryAllCourseListBySchoolId(schoolid,courseListId);
        this.repAllCourseList.DataSource = this.getPage(ds);
        this.repAllCourseList.DataBind();
    }

    public PagedDataSource getPage(DataSet ds)
    {
        PagedDataSource pds = new PagedDataSource();
        if (ds == null)
        {
            this.pager.RecordCount = 0;
            pds.DataSource = null;
        }
        else
        {
            this.pager.RecordCount = ds.Tables[0].Rows.Count;
            pds.DataSource = ds.Tables[0].DefaultView;
        }
   

        pds.AllowPaging = true;
        pds.CurrentPageIndex = pager.CurrentPageIndex - 1;
        pds.PageSize = pager.PageSize;
        return pds;
    }


    //根据时间段查找
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.txtBeginDate.Text.Trim().ToString() =="" ) 
        {

            this.lblStarDate.Text = "请输入上课日期";
        }
        if (this.txtendDate.Text.Trim().ToString() =="")
        {

            this.txtendDate.Text = "请输入结束日期";
        }
        else
        {

            DateTime beginDate = Convert.ToDateTime(this.txtBeginDate.Text.Trim().ToString());
            DateTime endDate = Convert.ToDateTime(this.txtendDate.Text.Trim().ToString());
            int id = this.CurrentUser.SchoolID;
            int courseId = 0;
            if (string.IsNullOrEmpty(this.courseListId)) courseId = 0;
            else
            {
                courseId = int.Parse(this.courseListId);
            }
            DataSet ds = CM.QueryByCourseBeginDate(id, courseId, beginDate, endDate);
            //ds = CM.QueryByCourseBeginDate(id, beginDate, endDate);
            if (ds.Tables[0].Rows.Count > 0)
            {
                this.repAllCourseList.DataSource = this.getPage(ds);
                this.repAllCourseList.DataBind();
            }
            else
            {

                this.repAllCourseList.DataSource = this.getPage(null); ;
              

            }
        }
        
        
    }

    //修改
    public void lbUpdate_Click(object sender, CommandEventArgs e)
    {
        Response.Redirect("UpdateCourseList.aspx?ID=" + e.CommandArgument.ToString());
    }
    protected void btnAddCourseList_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddCourseList.aspx");
    }
    protected void btnQuantityCourseList_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuantityRegistCourseList.aspx");
    }
    protected void lnkDel_Click(object sender, EventArgs e)
    {
       if(CM.DeleteCourseList(hfCheckItem.Value.Remove(hfCheckItem.Value.Length - 1, 1))>0)
       {
           JsHelper.AlertAndRedirect("删除成功！", "SearchCourseAllList.aspx");

       }
       else
       {
           JsHelper.Alert("删除失败！");
       }

        //Response.Write(hfCheckItem.Value.Remove(hfCheckItem.Value.Length - 1, 1)); 
    }
}
